package com.zidian.teacher.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.zidian.teacher.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;



/**
 * <p>
 * class desc: 相机工具
 * Copyright: Copyright(c) 2016
 * </p>
 */
public class CameraPicTureUtil {

    public static final int Cameta_pcaTure_1000 = 1000;
    public static final int Cameta_pcaTure_2000 = 2000;
    public static final int Cameta_pcaTure_3000 = 3000;

    public static void setPicToView(Context context, Bitmap mBitmap, String avaterPath) {
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
            //请插入内存卡再试
            ToastUtil.showToast(context, "请插入内存卡");
            return;
        }
        FileOutputStream b = null;

        File file1 = new File(avaterPath);
        file1.delete();

        File file = new File(avaterPath);
        try {
            file.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        try {
            b = new FileOutputStream(avaterPath);
            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭流
                b.flush();
                b.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 调用系统的裁剪
     *
     * @param uri
     */
    public static void cropPhoto(Activity activity, Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 120);
        intent.putExtra("outputY", 120);
        intent.putExtra("return-data", true);
        activity.startActivityForResult(intent, Cameta_pcaTure_3000);
    }

    public static String takePhotoImage;
    private static AlertDialog pictToTalkDialog;

    /**
     * 选择照片获取从相机拍照
     */
    public static void pictureToTalk(Context context) {
        final Activity activity = (Activity) context;
        pictToTalkDialog = CommonDialogUtil.getBottomDialog(context);
        Window window = pictToTalkDialog.getWindow();
        ListView dialogListView = (ListView) window.findViewById(R.id.dialogListView);
        dialogListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                switch (position) {
                    case 0:
                        takePhotoImage = Environment
                                .getExternalStorageDirectory().getPath()
                                + "/DCIM/Camera/"
                                + "aa"
                                + DateUtil.getCurrentDate("yyyyMMddHHmmss")
                                + ".jpg";
                        Intent intent2 = new Intent(
                                MediaStore.ACTION_IMAGE_CAPTURE);
                        intent2.putExtra(MediaStore.EXTRA_OUTPUT,
                                Uri.fromFile(new File(takePhotoImage)));
                        activity.startActivityForResult(intent2, Cameta_pcaTure_2000);// 采用ForResult打开
                        pictToTalkDialog.dismiss();
                        break;
                    case 1:
                        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                        intent1.setDataAndType(
                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                "image/*");
                        activity.startActivityForResult(intent1, Cameta_pcaTure_1000);
                        pictToTalkDialog.dismiss();
                        break;
                    default:
                        break;
                }
            }
        });

        ArrayList<BottomDialogEntity> bottomDialogEntities = new ArrayList<>();
        bottomDialogEntities.add(new BottomDialogEntity("0", "拍照"));
        bottomDialogEntities.add(new BottomDialogEntity("1", "从相册选择"));

        BottomDialogAdapter adapter = new BottomDialogAdapter(context, bottomDialogEntities);
        dialogListView.setAdapter(adapter);

    }

}
